<template>
	<view class="home">
		<navbar :isSearch="true" v-model='value' @inputChange="inputChange"></navbar>
		<view class="home-list">
			<view v-if="is_history" class="label-box">
				<view class="label-header">
					<text class="label-history">搜索历史</text>
					<text class="label-clear" @click="clear">清空</text>
				</view>
				<view v-if="historyLists.length>0" class="label-content">
					<view @click='openHistory(item)' v-for="item in historyLists" class="label-content_item">
						{{item.name}}
					</view>
				</view>
				<view v-else class="no-data">
					没有数据
				</view>
			</view>
			<list-scroll v-else class="list-scroll">
				<uni-load-more v-if='loading' status="loading" iconType="snow"></uni-load-more>
				<view v-if="searchList.length>0">
					<list-card @click='setHistory' v-for="(item,index) in searchList" :key="item._id" :item="item">
					</list-card>
				</view>
				<view v-if="searchList.length===0 && !loading">
					没有相关数据
				</view>
			</list-scroll>
		</view>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex'
	export default {
		data() {
			return {
				// historyList: []
				is_history: true,
				searchList: [],
				value: '',
				loading: false
			}
		},
		computed: {
			...mapState(['historyLists'])
		},
		onLoad() {},
		methods: {
			inputChange(val) {
				this.value = val
				if (!val) {
					clearTimeout(this.timer)
					this.mark = false
					this.getSearch(val)
					return
				}
				console.log("接受内容:" + val)
				// this.getList(val)
				if (!this.mark) {
					this.mark = true;
					this.timer = setTimeout(res => {
						this.mark = false;
						this.getSearch(val)
					}, 1000)
				}
			},
			clear() {
				this.$store.dispatch('clear_history')
				uni.showToast({
					title:"清空成功"
				})
			},
			getSearch(value) {
				if (!value) {
					this.searchList = []
					this.is_history = true
					return
				}
				this.loading = true
				this.is_history = false
				this.$api.get_search({
					value,
				}).then(res => {
					const {
						data
					} = res
					this.searchList = data;
					this.loading = false
					console.log(res)
				}).catch(() => {
					this.loading = false
				})
			},
			setHistory(item) {

				console.log('item:'+item)

				console.log(this.$store.dispatch('set_history', {
					name: this.value
				}))
			},
			openHistory(item) {
				this.value = item.name
				this.getSearch(this.value)
				console.log("this.value:" + this.value)
			}
		}
	}
</script>

<style lang="scss">
	page {
		height: 100%;
		display: flex;
		background-color: #f5f5f5;
	}

	.home {
		display: flex;
		flex-direction: column;
		flex: 1;
		border: red 1px solid;

		.home-list {
			.label-box {
				background-color: #fff;
				margin-bottom: 10px;

				.label-header {
					display: flex;
					justify-content: space-between;
					font-size: 14px;
					color: #666;
					padding: 10px 15px;
					border-bottom: 1px #f5f5f5 solid;

					.label-title {
						color: $mk-base-color;
					}

					.lable-clear {
						color: #30b33a;
					}
				}

				.label-content {
					display: flex;
					flex-wrap: wrap;
					padding: 15rpx;
					padding-top: 0;

					.label-content_item {
						padding: 2px 10rpx;
						margin-top: 12px;
						margin-right: 12px;
						border-radius: 5rpx;
						border: red 1px solid;
					}

				}

				.no-data {
					height: 200px;
					line-height: 200px;
					width: 100%;
					text-align: center;
					font-size: 12px;
					color: #666;

					font {
						color: #007AFF;
					}
				}
			}
		}

		.list-scroll {
			height: 100%;
		}

	}
</style>
